import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class UserSubSystem {

    private List<Student> studentList  = new ArrayList<>();
    private List<Organizer> organizerList = new ArrayList<>();
    private List<Teacher> teacherList = new ArrayList<>();

    public void addStudent(Student student){
        studentList.add(student);
    }

    public void removeStudentById(String id){
        Iterator<Student> studentIterator = studentList.iterator();
        while(studentIterator.hasNext()){
            Student student = studentIterator.next();
            if(student.getId().equals(id)){
                studentIterator.remove();
            }
        }
    }

    public Student getStudentById(String id){
        for (Student student:studentList) {
            if(student.getId().equals(id)){
                return student;
            }
        }
        return null;
    }

    public List<Student> getStudentList() {
        return studentList;
    }

    public void addOrganizer(Organizer organizer){
        organizerList.add(organizer);
    }

    public void removeOrganizerById(String id){
        Iterator<Organizer> organizerIterator = organizerList.iterator();
        while(organizerIterator.hasNext()){
            Organizer organizer = organizerIterator.next();
            if(organizer.getId().equals(id)){
                organizerIterator.remove();
            }
        }
    }

    public Organizer getOrganizerById(String id){
        for (Organizer organizer:organizerList) {
            if(organizer.getId().equals(id)){
                return organizer;
            }
        }
        return null;
    }

    public List<Organizer> getOrganizerList() {
        return organizerList;
    }

    public void addTeacher(Teacher teacher){
        teacherList.add(teacher);
    }

    public void removeTeacherById(String id){
        Iterator<Teacher> teacherIterator = teacherList.iterator();
        while(teacherIterator.hasNext()){
            Teacher teacher = teacherIterator.next();
            if(teacher.getId().equals(id)){
                teacherIterator.remove();
            }
        }
    }

    public Teacher getTeacherById(String id){
        for (Teacher teacher:teacherList) {
            if(teacher.getId().equals(id)){
                return teacher;
            }
        }
        return null;
    }

    public List<Teacher> getTeacherList() {
        return teacherList;
    }

    public void createTeam(String creatorId, String teamId, String teamName, String department){
        for (Student student:studentList) {
            if(student.getId().equals(creatorId)){
                Team team = new Team(teamId, teamName, department, student);
                student.getTeamList().add(team);
                break;
            }
        }
    }

    public void addStudentToTeam(String creator, String studentId, String teamId){
        for (Student student:studentList) {
            List<Team> teams = student.getTeamList();
            for (Team team:teams) {
                if(team.getTeamId().equals(teamId)){
                    for (Student student1:studentList) {
                        if(student1.getId().equals(studentId)){
                            team.students.add(student1);
                            break;
                        }
                    }
                }
            }
        }
    }

    public void addTeacherToTeam(String creatorId, String teacherId, String teamId){
        for (Student student:studentList) {
            List<Team> teams = student.getTeamList();
            for (Team team:teams) {
                if(team.getTeamId().equals(teamId)){
                    for (Teacher teacher:teacherList) {
                        if(teacher.getId().equals(teacherId)){
                            team.teachers.add(teacher);
                            break;
                        }
                    }
                }
            }
        }
    }
}
